# -*- coding:utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from . import treeItem
from com.mat.rpa.utils.globalConstants import GlobalConstants
from com.mat.rpa.views.workWindow.middlePanel.directives.condition import conditionSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.circle import circleSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.wait import waitSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.similarElement import similarElementSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.webAuto import webAutoSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.desktopAuto import desktopAutoSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.mouseKeyboard import mouseAndKeyboardSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.dataTable import dataTableSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.officeAuto import officeAutoSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.dialogOperation import dialogOperationSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.dataProcess import dataProcessingSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.operatingSystem import operatingSystemSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.flowOrApp import flowOrAppSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.AI import AISubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.networkOperation import networkOperationSubDirectiveTree
from com.mat.rpa.views.workWindow.middlePanel.directives.otherOperation import otherOperationSubDirectiveTree
class DirectiveTree(QTreeWidget):
    picPath = "./com/mat/rpa/views/workWindow/images/"
    def __init__(self, parent=None):
        super(DirectiveTree, self).__init__(parent)
        self.setColumnCount(2)
        # self.setHeaderLabels(["",""])
        self.setHeaderHidden(True) #隐藏标题头
        self.setStyleSheet("QTreeView::item {height: 30px;};")
        # 获得屏幕尺寸
        desktop = QApplication.desktop()
        rect = desktop.availableGeometry()
        screenWidth = rect.width()
        self.LeftWidth = screenWidth * 0.25
        self.setColumnWidth(0, self.LeftWidth*0.74)
        self.setColumnWidth(1, self.LeftWidth * 0.22)
        #添加我收藏的指令节点
        self.favoriteNode = self.createTreeNode(GlobalConstants.treeBranchType, self, "favorite", "我收藏的指令",
                            QIcon(DirectiveTree.picPath+"favorites.png"))
        #添加自定义指令节点
        self.selfDefindeDirectiveNode= self.createTreeNode(GlobalConstants.treeBranchType, self, "selfDefinedDirective", "自定义指令",
                            QIcon(DirectiveTree.picPath+"selfDefinedDirective.png"))
        self.getDirectiveBtn = QPushButton()
        self.getDirectiveBtn.setIcon(QIcon(DirectiveTree.picPath+"add.png"))
        self.getDirectiveBtn.setText("获取指令")
        self.getDirectiveBtn.setStyleSheet("border:none")
        self.getDirectiveBtn.clicked.connect(self.handleGetDirectiveClicked)
        self.setItemWidget(self.selfDefindeDirectiveNode, 1, self.getDirectiveBtn)
        #向指令树添加条件判断子节点
        conditionSubDirectiveTree.ConditionSubDirectiveTree(self)
        #向指令树添加循环子节点
        circleSubDirectiveTree.CircleSubDirectiveTree(self)
        # #向指令树添加等待子节点
        waitSubDirectiveTree.WaitSubDirectiveTree(self)
        #向指令树添加相似元素操作子节点
        similarElementSubDirectiveTree.SimilarElementSubDirectiveTree(self)
        #挂载网页自动化子节点
        webAutoSubDirectiveTree.WebAutoSubDirectiveTree(self)
        #桌面软件自动化子节点
        desktopAutoSubDirectiveTree.DesktopAutoSubDirectiveTree(self)
        #挂载鼠标键盘子节点
        mouseAndKeyboardSubDirectiveTree.MouseAndKeyboardSubDirectiveTree(self)
        #挂载数据表格子节点
        dataTableSubDirectiveTree.DataTableSubDirectiveTree(self)
        #挂载office自动化子节点
        officeAutoSubDirectiveTree.OfficeAutoSubDirectiveTree(self)
        #挂载对话框操作子节点
        dialogOperationSubDirectiveTree.DialogOperationSubDirectiveTree(self)
        #挂载数据处理子节点
        dataProcessingSubDirectiveTree.DataProcessingSubDirectiveTree(self)
        #挂载操作系统操作子节点
        operatingSystemSubDirectiveTree.OperatingSystemSubDirectiveTree(self)
        #挂载流程或应用操作子节点
        flowOrAppSubDirectiveTree.FlowOrAppSubDirectiveTree(self)
        #挂载人工智能操作子节点
        AISubDirectiveTree.AISubDirectiveTree(self)
        #挂载网络操作子节点
        networkOperationSubDirectiveTree.NetworkOperationSubDirectiveTree(self)
        #挂载其它操作子节点
        otherOperationSubDirectiveTree.OtherOperationSubDirectiveTree(self)

    def createTreeNode(self, nodeType, parentNode, directiveType, text, icon=None):
        node = treeItem.TreeItemWithID(parentNode)
        node.setNodeType(nodeType)
        node.setDirectiveType(directiveType)
        node.setText(0, text)
        if icon != None:
            node.setIcon(0, icon)
        return node

    def handleGetDirectiveClicked(self):
        print("点击了获取指令")

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.item = self.itemAt(event.pos())#####
            if self.item:
                print("mousePressEvent 当前选中", self.item.text(0))
            else:
                print("mousePressEvent 没有选中")
        super(DirectiveTree, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            if self.item:
                mimeData = QMimeData()
                mimeData.setText(self.item.nodeType+"|"+self.item.directiveType+"|"+self.item.text(0))
                drag = QDrag(self)
                drag.setMimeData(mimeData)
                drag.exec_(Qt.MoveAction)
        super(DirectiveTree, self).mouseMoveEvent(event)

